//
//  ChangeRecordView.m
//  BiologyLibrary
//
//  Created by 朱进 on 16/7/16.
//  Copyright © 2016年 youxh. All rights reserved.
//

#import "ChangeRecordView.h"

@implementation ChangeRecordView

-(id) initWithFrame:(CGRect)frame type:(NSInteger)type recordId:(NSString *)recordId {

    self = [super initWithFrame:frame];
    if (self) {
        
        UIView *backView = [[UIView alloc] initWithFrame:
                            CGRectMake(0, 0, frame.size.width, frame.size.height)];
        backView.backgroundColor = [UIColor blackColor];
        backView.alpha = 0.3;
        backView.userInteractionEnabled = NO;
        [self addSubview:backView];
        
        _contentArray = [Record findWithFormat:@"WHERE %@ = %d AND recordId != '%@'",@"type",
                         type, recordId];

        UITableView *contentTableView = [[UITableView alloc] initWithFrame:
                                         CGRectMake(0,
                                                    self.frame.size.height / 2,
                                                    self.frame.size.width,
                                                    self.frame.size.height / 2)
                                                                     style:UITableViewStyleGrouped];
        contentTableView.backgroundColor = [UIColor colorWithRed:0.16 green:0.19 blue:0.24 alpha:1.0];
        contentTableView.dataSource = self;
        contentTableView.delegate = self;
        [self addSubview:contentTableView];
    }
    return self;
}

#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [_contentArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *strIdentifier = [NSString stringWithFormat:@"strIdentifier%ld",(long)indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strIdentifier];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                      reuseIdentifier:strIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.backgroundColor = [UIColor clearColor];
        
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.font = [UIFont flatFontOfSize:g_fSize_AppFont];

        cell.detailTextLabel.textColor = [UIColor lightGrayColor];
        cell.detailTextLabel.font = [UIFont flatFontOfSize:g_fSize_AppFont - 3];
    }
    
    Record *tempRecord = (Record *)[_contentArray objectAtIndex:indexPath.row];
    cell.textLabel.text = tempRecord.recordId;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"第%ld步",tempRecord.savedStepCount];

    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return fHeight_Nav;
}

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    CGFloat fHeaderViewHeight = [self tableView:tableView
                       heightForHeaderInSection:section];
    
    UIView *headView = [[UIView alloc] initWithFrame:
                          CGRectMake(0, 0, tableView.frame.size.width , fHeaderViewHeight)];
    headView.backgroundColor = [UIColor clearColor];

    UILabel *titleLabel_ = [[UILabel alloc] initWithFrame:
                            CGRectMake(10, 0, headView.frame.size.width - 20,
                                       headView.frame.size.height)];
    titleLabel_.backgroundColor = [UIColor clearColor];
    titleLabel_.font = [UIFont flatFontOfSize:g_fSize_AppFont];
    titleLabel_.textColor = [UIColor whiteColor];
    titleLabel_.textAlignment = NSTextAlignmentCenter;
    titleLabel_.text = @"更换样本采集";
    [headView addSubview:titleLabel_];
    
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    CGFloat fSectionFootHeight = 0.1;
    
    if ([_contentArray count] == 0) {
        
        fSectionFootHeight = tableView.frame.size.height - fHeight_Nav;
    }
    
    return fSectionFootHeight;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    CGFloat fFootViewHeight = [self tableView:tableView
                     heightForFooterInSection:section];
    
    UILabel *noResultLabel = [[UILabel alloc] initWithFrame:
                              CGRectMake(0,0,
                                         tableView.frame.size.width,
                                         fFootViewHeight)];
    noResultLabel.backgroundColor = [UIColor clearColor];
    noResultLabel.font = [UIFont flatFontOfSize:g_fSize_AppFont];
    noResultLabel.textColor = [UIColor whiteColor];
    noResultLabel.textAlignment = 1;
    noResultLabel.text = @"暂无记录";

    if ([_contentArray count] == 0) {
        noResultLabel.hidden=NO;
    }else{
        noResultLabel.hidden=YES;
    }

    
    return noResultLabel;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.delegate &&
        [self.delegate respondsToSelector:@selector(changeRecordView:didSelecredRecord:)]) {
        
        Record *tempRecord = (Record *)[_contentArray objectAtIndex:indexPath.row];
        [self.delegate changeRecordView:self didSelecredRecord:tempRecord];
    }
}

@end
